<jsp:useBean id="sessiondata" class="bibbletex.models.SessionData"
	scope="session" />

<%@ page import="bibbletex.models.Collaboration, bibbletex.models.Reference,java.util.UUID,java.util.List,java.util.Collections"
	import="bibbletex.views.CollaborationView,bibbletex.controllers.CollaborationController, bibbletex.views.ReferenceView, java.util.ArrayList"%>
	
	<%
	String page_title;
	String onloadjsfunction = "changeAttributes()";
	String onchangeformjscode="changeAttributes()";
	Collaboration c;
	String id_parameter = request.getParameter("cid");
	String sorting = request.getParameter("sorting");
	String query = request.getParameter("q");
	boolean show_library = true;
	int counter = 0;
	boolean show_navigation = false;
	String[] jstitle={"collab"};

	if (id_parameter == null) {
		page_title = new String("Collaboration saved!");
		c = CollaborationController.load(sessiondata.collaboration.getId().toString());
		sessiondata.collaboration = c;
	} else {
		page_title = new String("Show collaboration");

		// the collaboration is editable if it was already editable and the public ID matches
		// (prevents changing from a previously editable collaboration to a new suggestion-only
		// collaboration)
		// or if the id parameter is simply the private id
		sessiondata.collaboration_is_editable = (sessiondata.collaboration_is_editable && sessiondata.collaboration
				.getPublic_id().toString().equals(id_parameter))
				|| Collaboration.id_is_private(id_parameter);

		c = CollaborationController.load(id_parameter);
		sessiondata.collaboration = c;
	}
	if (c != null){
		show_navigation = true;
	}
	else{
		show_navigation = false;
	}

	if (request.getParameter("show_references") != null
			&& request.getParameter("show_references").equals(
					"suggestions"))
		show_library = false;
%>

<%! boolean show_title = true;%>


			<%
				List<Reference> refs = new ArrayList<Reference>();
					if (show_library){
						if (sessiondata.collaboration_is_editable || c.getPublic_library() == true)	
							refs = c.getReferences();
					}
					else
						refs = c.getSuggestions();
					
					
					CollaborationView.sort(refs, sorting);
					
					for (Reference r : refs) {
						counter = 0;
						if(r.contains(query)){
			%>
			
			<div class="refColumn clearfix <%=r.getType().toString().toLowerCase()%>" id="cr<%=r.getId()%>">
				<div class="type<% if (!sessiondata.collaboration_is_editable){ %> public<% } %>"><% if (sessiondata.collaboration_is_editable){ %><input type="checkbox" name="rid" value="<%=r.getId()%>" style="float:left;" /><% } %><%=r.getType().toString().toLowerCase()%></div>
				<% if(r.getTitle() != null){ %>
					<% if(r.getTitle().length() > 20) {%>
					<div class="title<% if (!sessiondata.collaboration_is_editable){ %> public<% } %>"><%=r.getTitle().substring(0,20).toLowerCase()%>...</div>
					<%} else{ %>
					<div class="title<% if (!sessiondata.collaboration_is_editable){ %> public<% } %>"><%=r.getTitle()%></div>
				<% }} else{ %>
					<div class="title<% if (!sessiondata.collaboration_is_editable){ %> public<% } %>">No Data</div>
				<% } if(r.getAuthors() != null){ %>
					<% if(r.getAuthors().length() > 20) {%>
					<div class="author<% if (!sessiondata.collaboration_is_editable){ %> public<% } %>"><%=r.getAuthors().substring(0,20)%>...</div>
					<%} else{ %>
					<div class="author<% if (!sessiondata.collaboration_is_editable){ %> public<% } %>"><%=r.getAuthors()%></div>
					<%} %>
				<% }else{ %>
					<div class="author<% if (!sessiondata.collaboration_is_editable){ %> public<% } %>">No Data</div>
				<% } %>
				
				<div class="rating<% if (!sessiondata.collaboration_is_editable){ %> public<% } %>" id="ratingr<%=r.getId().toString()%>">
						<jsp:include page="/collaboration/reference/rate.jsp">
    						<jsp:param name="rid" value="<%=r.getId()%>"/>
						</jsp:include>
				</div>
				<% if (sessiondata.collaboration_is_editable){ %>
				<div class="controls">
				<form name="controls<%=r.getId()%>">
					<%
						if (show_library) {
					%>
					<div class="edit clickable"><strong
						onclick="showPopUp2('<%=r.getId()%>');"
						title="Edit Reference"></strong></div>
					<%
						} else {
					%>
					<div class="accept clickable" id="accept<%=r.getId()%>" 
						 onclick="xmlhttpPost_('/collaboration/reference/accept.jsp', 'controls<%=r.getId()%>', 'accept<%=r.getId()%>', '<img src=\'/img/wait.gif\'>', true,'rid=<%=r.getId()%>'); hideReference('cr<%=r.getId()%>');">
					</div>
					<%
						}
					%>
					<div class="delete clickable" id="delete<%=r.getId()%>" 
						 onclick="xmlhttpPost_('/collaboration/reference/delete.jsp', '', 'delete<%=r.getId()%>', '<img src=\'/img/wait.gif\'>', true,'rid=<%=r.getId()%>'); hideReference('cr<%=r.getId()%>');">
					</div>
				</form>
				</div>	
				<% } %>
				
				
				
				<div class="refDetails" id="r<%=r.getId().toString()%>" style="display:none; height:0px;">
					<% if(r.getTitle() != null && !r.getTitle().equals("null") && r.getTitle().length() > 20){ counter++; %>
						<em>Title: <%=r.getTitle()%></em><br />
					<% } if(r.getAuthors() != null && !r.getAuthors().equals("null") && r.getAuthors().length() > 20){ counter++; %>
						<em>Authors: <%=r.getAuthors()%></em><br />
					<% } if(r.getBooktitle() != null && !r.getBooktitle().equals("null")){ counter++; %>
						<em>Booktitle: <%=r.getBooktitle()%></em><br />
					<% } if(r.getEditor() != null && !r.getEditor().equals("null")){ counter++; %>
						<em>Editor: <%=r.getEditor()%></em><br />
					<% } if(r.getPublisher() != null && !r.getPublisher().equals("null")){ counter++; %>
						<em>Publisher: <%=r.getPublisher()%></em><br />
					<% } if(r.getOrganization() != null && !r.getOrganization().equals("null")){ counter++; %>
						<em>Organization: <%=r.getOrganization()%></em><br />
					<% } if(r.getSchool() != null && !r.getSchool().equals("null")){ counter++; %>
						<em>School: <%=r.getSchool()%></em><br />
					<% } if(r.getJournal() != null && !r.getJournal().equals("null")){ counter++; %>
						<em>Journal: <%=r.getJournal()%></em><br />
					<% } if(r.getYear() != null && !r.getYear().equals("null")){ counter++; %>
						<em>Year: <%=r.getYear()%></em><br />
					<% } if(r.getVolume() != null && !r.getVolume().equals("null")){ counter++; %>
						<em>Volume: <%=r.getVolume()%></em><br />
					<% } if(r.getNumber() != null && !r.getNumber().equals("null")){ counter++; %>
						<em>Number: <%=r.getNumber()%></em><br />
					<% } if(r.getPages() != null && !r.getPages().equals("null")){ counter++; %>
						<em>Pages: <%=r.getPages()%></em><br />
					<% } if(r.getMonth() != null && !r.getMonth().equals("null")){ counter++; %>
						<em>Month: <%=r.getMonth()%></em><br />
					<% } if(r.getNote() != null && !r.getNote().equals("null")){ counter++; %>
						<em>Note: <%=r.getNote()%></em><br />
					<% } if(r.getSeries() != null && !r.getSeries().equals("null")){ counter++; %>
						<em>Series: <%=r.getSeries()%></em><br />
					<% } if(r.getAddress() != null && !r.getAddress().equals("null")){ counter++; %>
						<em>Address: <%=r.getAddress()%></em><br />
					<% } if(r.getEdition() != null && !r.getEdition().equals("null")){ counter++; %>
						<em>Edition: <%=r.getEdition()%></em><br />
					<% } if(r.getIsbn() != null && !r.getIsbn().equals("null")){ counter++; %>
						<em>ISBN: <%=r.getIsbn()%></em><br />
					<% } if(r.getHowpublished() != null && !r.getHowpublished().equals("null")){ counter++; %>
						<em>Howpublished: <%=r.getHowpublished()%></em><br />
					<% } %>
					
				</div>
				
					
				<strong onclick="showDetails('r<%=r.getId().toString()%>',<%=counter%>)"  id="br<%=r.getId().toString()%>" class="showDetails2 clickable">Show Details &#x25BC;</strong>
			</div>

			<%
				}
			}
			%>

			
